﻿package cn.antscript.airCMS.display.component 
{
	import cn.antscript.airCMS.data.FilesData;
	import cn.antscript.airCMS.data.ImgsData;
	import cn.antscript.airCMS.data.MusicsData;
	import cn.antscript.airCMS.data.PageData;
	import cn.antscript.airCMS.data.singleData.AbstractData;
	import cn.antscript.airCMS.data.TextsData;
	import cn.antscript.airCMS.data.VideosData;
	import cn.antscript.airCMS.data.XMLData;
	import cn.antscript.airCMS.display.component.btnbase.FileBtn;
	import cn.antscript.airCMS.display.component.btnbase.GroupBtn;
	import cn.antscript.airCMS.display.component.btnbase.IcoBitmapdatas;
	import cn.antscript.airCMS.display.component.btnbase.ImgBtn;
	import cn.antscript.airCMS.display.component.btnbase.TextBtn;
	import com.bit101.components.PushButton;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	/**
	 * ...
	 * @author antscript
	 */
	public class AbstractItemContainer extends Sprite
	{
		private var _frameWidth:int = 700;
		private var _frameHeight:int = 400;
		
		
		private var _pageIndex:int;
		private var _typeIndex:String;
		private var _groupIndex:int;
		private var _itemIndex:int;
		private var _changeStr:String;
		
		private var _container:Sprite;
		private var _scrollPanel:ScrollPanel;
		
		private var _targetData:Object;
		private var _replaceData:Object;
		private var _isReplace:Boolean;
		
		private var _menu:Menu;
		private var _typeContainer:TypeContainer;
		
		
		private var _retBtn:PushButton;
		private var _addBtn:PushButton;
		
		public function AbstractItemContainer() 
		{
			_menu = new Menu();
			_menu.x = -7;
			_menu.y = -40;
			addChild(_menu);
			_menu.addEventListener("pageChange", onMenuChange);
			_menu.addEventListener("typeChange", onMenuChange);
			_menu.addEventListener("groupChange", onMenuChange);
			
			_container = new Sprite();
			
			_scrollPanel = new ScrollPanel(IcoBitmapdatas.ScrollBarHander, this, 0, 0);			
			_scrollPanel.setup(_container, _frameWidth, _frameHeight);
			
			_typeContainer = new TypeContainer();
			_typeContainer.x = 150;
			_typeContainer.y = 150;
			addChild(_typeContainer);
			_typeContainer.visible = false;
			
			_retBtn = new PushButton(this, 615, 420, "上一级");
			_retBtn.addEventListener(MouseEvent.CLICK, onRet);
			_retBtn.visible = false;
			
			_addBtn = new PushButton(this, 0, 420, "新增");
			_addBtn.addEventListener(MouseEvent.CLICK,onAdd);
			_addBtn.visible = false;
			
			AddPanel.getInstance().addEventListener(AddPanel.ADD_COM, onAdded);
			
			this.addEventListener("replace", onReplace,true);
		}
		
		private function onMenuChange(e:Event):void 
		{
			switch(e.type) {
				case "pageChange":
				_pageIndex = _menu.pageIndex;
				fillData(XMLData.getInstance().pagesData[_pageIndex]);
				break;
				case "typeChange":
				_typeIndex = _menu.typeIndex;
				fillData(XMLData.getInstance().pagesData[_pageIndex][_typeIndex]);
				break;
				case "groupChange":
				_groupIndex = _menu.groupIndex;
				fillData(XMLData.getInstance().pagesData[_pageIndex][_typeIndex][_groupIndex]);
				break;
			}
		}
		
		private function onReplace(e:Event):void 
		{
			_isReplace = true;
			_replaceData = e.target.data;
			AddPanel.getInstance().replace(_replaceData as AbstractData,XMLData.getInstance().pagesData[_pageIndex][_typeIndex][_groupIndex], XMLData.getInstance().pagesData[_pageIndex].id, this.stage);
		}
		
		private function onAdded(e:Event):void 
		{
		
			if (!_isReplace) {
				if (AddPanel.getInstance().isNewGroup) {
					XMLData.getInstance().pagesData[_pageIndex][_typeIndex].push(AddPanel.getInstance().absData);
					_groupIndex++;
					fillData(XMLData.getInstance().pagesData[_pageIndex][_typeIndex][_groupIndex]);
				}
				else {
					XMLData.getInstance().pagesData[_pageIndex][_typeIndex][_groupIndex]["container"].push(AddPanel.getInstance().absData);
					fillData(XMLData.getInstance().pagesData[_pageIndex][_typeIndex][_groupIndex]);
				}
			}
			else {				
				fillData(XMLData.getInstance().pagesData[_pageIndex][_typeIndex][_groupIndex]);
			}
			
			
			
			
		}		
		private function onAdd(e:MouseEvent):void 
		{
			_isReplace = false;
			AddPanel.getInstance().add(XMLData.getInstance().pagesData[_pageIndex][_typeIndex][_groupIndex], XMLData.getInstance().pagesData[_pageIndex].id, this.stage);
			
		}
		
		private function onRet(e:MouseEvent):void 
		{
			var data:Object = _targetData;trace(_targetData)
			
			if (data is Vector.<*>) {
				fillData(XMLData.getInstance().pagesData[_pageIndex]);
			}
			else if (data is PageData) {
				fillData(XMLData.getInstance());	
			}
			else if (data is FilesData) {
				fillData(XMLData.getInstance().pagesData[_pageIndex][_typeIndex]);	
			}
			else if (data is ImgsData) {
				fillData(XMLData.getInstance().pagesData[_pageIndex][_typeIndex]);	
			}
			else if (data is MusicsData) {
				fillData(XMLData.getInstance().pagesData[_pageIndex][_typeIndex]);	
			}
			else if (data is TextsData) {
				fillData(XMLData.getInstance().pagesData[_pageIndex][_typeIndex]);	
			}
			else if (data is VideosData) {
				fillData(XMLData.getInstance().pagesData[_pageIndex][_typeIndex]);
				
			}
		}
		
		
		public function fillData(data:Object):void {
			
			remove();
			_targetData = data;
			
			if (data is XMLData) {
				createPages();				
				_retBtn.visible = false;
				_addBtn.visible = false;
				_menu.hide();
			}
			else if (data is Vector.<*>) {
				createItems();
				_retBtn.visible = true;
				_addBtn.visible = false;
				_menu.fill(_pageIndex,_typeContainer,_typeIndex);
			}
			else if (data is PageData) {
				createTypes();
				_retBtn.visible = true;
				_addBtn.visible = false;
				_menu.fill(_pageIndex);
			}
			else if (data is FilesData) {
					createFiles();
				_retBtn.visible = true;
				_addBtn.visible = true;
				_menu.fill(_pageIndex,_typeContainer,_typeIndex, XMLData.getInstance().pagesData[_pageIndex][_typeIndex],_groupIndex);
			}
			else if (data is ImgsData) {
					createImgs();
				_retBtn.visible = true;
				_addBtn.visible = true;
				_menu.fill(_pageIndex,_typeContainer,_typeIndex, XMLData.getInstance().pagesData[_pageIndex][_typeIndex],_groupIndex);
			}
			else if (data is MusicsData) {
					createMusics();
				_retBtn.visible = true;
				_addBtn.visible = true;
				_menu.fill(_pageIndex,_typeContainer,_typeIndex, XMLData.getInstance().pagesData[_pageIndex][_typeIndex],_groupIndex);
			}
			else if (data is TextsData) {
					createTexts();
				_retBtn.visible = true;
				_addBtn.visible = true;
				_menu.fill(_pageIndex,_typeContainer,_typeIndex, XMLData.getInstance().pagesData[_pageIndex][_typeIndex],_groupIndex);
			}
			else if (data is VideosData) {
					createVideos();
				_retBtn.visible = true;
				_addBtn.visible = true;
				_menu.fill(_pageIndex,_typeContainer,_typeIndex, XMLData.getInstance().pagesData[_pageIndex][_typeIndex],_groupIndex);
			}
			
			_container.graphics.clear();
			_container.graphics.beginFill(0, 0);
			_container.graphics.drawRect(0, 0, _container.width, _container.height + 20);
			_container.graphics.endFill();
			
			_container.addEventListener("deleted", onDeleted,true);
			
			_scrollPanel.update();
		}
		
		private function onDeleted(e:Event):void 
		{
			XMLData.getInstance().pagesData[_pageIndex][_typeIndex][_groupIndex]["container"]["splice"](
				(XMLData.getInstance().pagesData[_pageIndex][_typeIndex][_groupIndex]["container"]["indexOf"](e.target.data)), 1);
			fillData(XMLData.getInstance().pagesData[_pageIndex][_typeIndex][_groupIndex]);
		}
		
		
		private function createPages():void
		{
			for (var i:int = 0; i < _targetData.pagesData.length; i++) 
			{
				(new GroupBtn(XMLData.getInstance().pagesData[i], _container, 20, 35 * i+20)).addEventListener(MouseEvent.CLICK,onClick);
			}			
			_changeStr = "_pageIndex";
			_typeContainer.visible = false;
			
		}		
		private function createItems():void
		{
			for (var i:int = 0; i < _targetData.length; i++) 
			{
				(new GroupBtn(_targetData[i], _container, 20, 35 * i+20)).addEventListener(MouseEvent.CLICK,onClick);
			}
			_changeStr = "_groupIndex";
			_typeContainer.visible = false;
		}
		private function createTypes():void
		{
			//_container.addChild(_typeContainer);
			//_typeContainer.x = 150;
			//_typeContainer.y = 150;
			_typeContainer.show(_targetData);
			_typeContainer.addEventListener(MouseEvent.CLICK, onClick)
			_changeStr = "_typeIndex";
			_typeContainer.visible = true;
		}
		private function createFiles():void
		{
			
			for (var i:int = 0; i < (_targetData as FilesData).container.length; i++) 
			{
				new FileBtn(	(_targetData as FilesData).container[i],
								IcoBitmapdatas.FileType,
								(_targetData as FilesData).allowDel,
								_container, (i % 4) * 150+40, int(i / 4) * 200+20);
			}
			_changeStr = "_itemIndex";
			_typeContainer.visible = false;
		}
		
		private function createImgs():void
		{
			for (var i:int = 0; i < (_targetData as ImgsData).container.length; i++) 
			{
				new ImgBtn(	(_targetData as ImgsData).container[i],
								(_targetData as ImgsData).allowDel,
								_container, (i % 4) * 150+40, int(i / 4) * 200+20);
			}
			_changeStr = "_itemIndex";
			_typeContainer.visible = false;
		}
		
		private function createMusics():void
		{
			for (var i:int = 0; i < (_targetData as MusicsData).container.length; i++) 
			{
				new FileBtn(	(_targetData as MusicsData).container[i],
								IcoBitmapdatas.MusicType,
								(_targetData as MusicsData).allowDel,
								_container, (i % 4) * 150+40, int(i / 4) * 200+20);
			}
			_changeStr = "_itemIndex";
			_typeContainer.visible = false;
		}
		
		private function createTexts():void
		{
			for (var i:int = 0; i < (_targetData as TextsData).container.length; i++) 
			{
				new TextBtn((_targetData as TextsData).container[i], (_targetData as TextsData).allowDel,
							_container, 20, i * 30+20);
			}
			_changeStr = "_itemIndex";
			_typeContainer.visible = false;
		}
		
		private function createVideos():void
		{
			for (var i:int = 0; i < (_targetData as VideosData).container.length; i++) 
			{
				new FileBtn(	(_targetData as VideosData).container[i],
								IcoBitmapdatas.VideoType,
								(_targetData as VideosData).allowDel,
								_container, (i % 4) * 150+40, int(i / 4) * 200+20);
			}
			_changeStr = "_itemIndex";
			_typeContainer.visible = false;
		}
		
		
		private function remove():void {
			while (_container.numChildren > 0) {
				_container.getChildAt(0).removeEventListener(MouseEvent.CLICK,onClick)
				_container.removeChildAt(0);
			}
		}
		
		private function onClick(e:MouseEvent):void 
		{
			switch(_changeStr) {
				case "_pageIndex":
				_pageIndex = _container.getChildIndex(e.target as DisplayObject);
				fillData(XMLData.getInstance().pagesData[_pageIndex]);
				break;
				case "_typeIndex":
				_typeIndex = _typeContainer.typeStr;
				fillData(XMLData.getInstance().pagesData[_pageIndex][_typeIndex]);
				break;
				case "_groupIndex":
				_groupIndex = _container.getChildIndex(e.target as DisplayObject);
				fillData(XMLData.getInstance().pagesData[_pageIndex][_typeIndex][_groupIndex]);
				break;
				case "_itemIndex":
				_itemIndex = _container.getChildIndex(e.target as DisplayObject);
				fillData(XMLData.getInstance().pagesData[_pageIndex][_typeIndex][_groupIndex][_itemIndex]);
				break;
				default:
				break;
			}
			
			
		}
		
		protected function showBase():void {
			this.visible = true;
			
		}
		
		public function hide():void {
			
			this.visible = false;
		}
		
	}

}